﻿using Bricks.Business.Service;
using Bricks.Business.Service.CommonSupport;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils.Extensions;
using System.Configuration;

namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class MainController : Controller
    {
        //[Import]
        //public IMenuInfoService MenuInfoService { get; set; }

        //[Import]
        //public IUserMenuActionService UserMenuActionService { get; set; }

        [Import]
        public IUserInfoService UserService { get; set; }

        public ActionResult Index()
        {
            ////如果已经登录
            //if (User.Identity.IsAuthenticated)
            //{
            //    if (SystemData.Instance.CurrentUser == null)
            //    {
            //        var user = UserService.Users.FirstOrDefault(m => m.UserName == User.Identity.Name);
            //        if (user != null)
            //        {
            //            SystemData.Instance.CurrentUser = user;
            //        }
            //    }
            //    ViewBag.UserMenues = UserService.GetUserMenues(SystemData.Instance.CurrentUser);  //获取用户菜单
            //    return View();
            //}
            ////如果没有登录，则跳转到登录界面
            //else
            //{
            //    return RedirectToAction("Login", "Account");
            //}            

            //如果已经登录
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                ViewBag.UserMenues = UserService.GetUserMenues(SystemData.Instance.CurrentUser);  //获取用户菜单

                if (ConfigurationManager.AppSettings.AllKeys.Contains("AppName"))
                    ViewBag.AppName = ConfigurationManager.AppSettings["AppName"];  //应用名称
                else
                    ViewBag.AppName = "项目管理系统 1.0 BETA";  //应用名称

                if (ConfigurationManager.AppSettings.AllKeys.Contains("Copyright"))
                    ViewBag.Copyright = ConfigurationManager.AppSettings["Copyright"];  //版权信息
                else
                    ViewBag.Copyright = "COPYRIGHT © BRICKS ALL RIGHTS RESERVED";  //版权信息

                return View();
            }
            //如果没有登录，则跳转到登录界面
            else
            {
                return RedirectToAction("Login", "Account");
            }
        }

        /// <summary>
        /// 跳转菜单
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        public ActionResult TurnToMenu(string menuID)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                //ViewBag.UserMenues = UserService.GetUserMenues(SystemData.Instance.CurrentUser, menuID.ToGuid());
                //return View("Index");

                string menuContent = UserService.GetUserMenues(SystemData.Instance.CurrentUser, menuID.ToGuid());
                return Content(menuContent);
            }

            //如果没有登录，则跳转到登录界面
            else
            {
                return RedirectToAction("Login", "Account");
            }
        }

        public ActionResult Portal()
        {
            return View();
        }
    }
}
